#include <buola/chart/plots/cpatchplot.h>

#include <buola/image/cgc.h>
#include <buola/chart/cxychart.h>
#include <buola/chart/cxaxis.h>
#include <buola/chart/cyaxis.h>

namespace buola { namespace chart {

CPatchPlot::CPatchPlot()
    :   mColorMapper(new ELinearMapper<double,img::CColor>(0,1,img::CColor(L"black"),img::CColor(L"white")))
    
{
    mStyle=POINTFILL;
}

CPatchPlot::~CPatchPlot()
{
}

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
////////////////////////// Interface //////////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

void CPatchPlot::SetData(const mat::CMat_d &pSeries)
{
    mSeries=pSeries;
}

void CPatchPlot::SetColorMapper(EValueMapper<double,img::CColor> *pMapper)
{
    mColorMapper.reset(pMapper);
}

double CPatchPlot::GetMinX()
{
    return 0.0;
}

double CPatchPlot::GetMaxX()
{
    return mSeries.Cols();
}

double CPatchPlot::GetMinY()
{
    return 0.0;
}

double CPatchPlot::GetMaxY()
{
    return mSeries.Rows();
}

CRect_d CPatchPlot::GetExtraSpace()
{
    return CRect_d(0.0,0.0,0.0,0.0);
}

void CPatchPlot::Draw(CXYChartContext &pC)
{
    if(!mSeries.size()) return;

    for(int i=0;i<mSeries.Rows();i++)
    {
        for(int j=0;j<mSeries.Cols();j++)
        {
            CRect_d lRect(pC.mXAxis->Map(j+0.1),pC.mYAxis->Map(i+0.1),
                        pC.mXAxis->Map(j+0.9),pC.mYAxis->Map(i+0.9));
            lRect.Normalize();
            pC.mGC->Path().Rect(lRect.TopLeft(),lRect.Size());
            pC.mGC->SetSource(mColorMapper->Map(mSeries(i,j)));
            pC.mGC->FillPath();
        }
    }
}

/*namespace chart*/ } /*namespace buola*/ }
